var pageCount = 0;	//分页
var D = true;		//daily格式是否匹配
var E = true;		//employee格式是否匹配
//账单数据显示
function income_show(){
	$.ajax({
		url: "datalen?tableName=income",
		type: "POST",
		async: false,
		dataType: "json",
		success: function(date){
		pageCount = date;
		}
	}),
	//分页总数量
	$('.pagination').pagination(pageCount,
		{
		callback: income_PageCallback,		//PageCallback() 为翻页调用次数
		prev_text:"< 上一页",
		next_text:"下一页  >",
		items_per_page: 5,
		num_edge_entries: 0,		//两侧收尾分页条目数
		num_display_entries: 5,	//连续分页主题部分分页条目数
		current_page: 0,			//当前页索引
	});
}

//将数据读取出来
function income_PageCallback(page_index,jq){
	$.ajax({
		url: "do.income?method=0",
		type: "POST",
		async: false,
		dataType: "json",
		data: {'index':page_index + 1},
		success: function(data){
			var html = '<tr><th>账单编号</th><th>利润</th><th>部门</th><th>经手人</th><th>交易日期</th><th>最后修改日期</th><th>操作列</th></tr>';
			var list = data.incomes;
			for(var i=0;i<list.length;i++){
				html += '<tr><td>'+list[i].id+'</td><td>'
				+list[i].daily +'</td><td>'
				+list[i].department+'</td><td>'
				+list[i].employee+'</td><td>'
				+(new Date(list[i].bussiness)).getFullYear()+"-"+((new Date(list[i].bussiness)).getMonth()+1)+"-"+(new Date(list[i].bussiness)).getDate()+'</td><td>'
				+(new Date(list[i].lastMod)).getFullYear()+"-"+((new Date(list[i].lastMod)).getMonth()+1)+"-"+(new Date(list[i].lastMod)).getDate()+'</td><td>'
				+"<input type='button' value='修改' class='update' onclick='income_update_show("+list[i].id+")'></td></tr>"
			}
			$('.data_table').html(html);
			$('#button').html("<input type='button' id='addshow_button' value='添加' onclick='income_add_show()'>");
			$('.message_main_head').text('收入管理');
			pageCount = data.count;
		}
	});
}

//修改按钮事件，显示页面
function income_update_show(id){
	var incomeId = id;
	$.ajax({
		type:"POST",
		url:"do.income",
		data:"method=1&id="+id,
		dataType:"json",
		success:function(data){
			var income = data.update_income;
			var dep_show = data.depts_show;
			var html = "";
			//设置表头
			
			html = "<tr><th>账单编号</th><th>利润</th><th>部门</th><th>经手人</th><th>交易日期</th><th>最后修改日期</th></tr>"
			html += "<tr>";
			html += "<td><input type='text' value='"+income.id+"' disabled></td>";
			html += "<td><input type='text' id='daily' value='"+income.daily+"'></td>";
			//td表格单元中，添加一个部门下拉列表
			html += "<td><select id='deptName'>";
			for(var i=0;i<dep_show.length;i++){
				html += "<option value="+dep_show[i]+">"+dep_show[i]+"</option>";
			}
			html += "</select></td>";
			html += "<td><input type='text' id='employee' value='"+income.employee+"'></td>";
			html += "<td>"+(new Date(income.bussiness)).getFullYear()+"-"+((new Date(income.bussiness)).getMonth()+1)+"-"+(new Date(income.bussiness)).getDate()+"</td>";
			html += "<td>"+new Date().getFullYear()+"-"+(new Date().getMonth()+1)+"-"+new Date().getDate()+"</td>";
			html += "</tr>";
			
			$('.message_main_head').text('收入管理  > 修改');
			$('.data_table').html(html);
			//显示表格操作按钮，添加和重置
			$('#button').html("<input type='button' value='修改' onclick='income_update("+income.id+")'/><input type='button' value='重置' onclick='income_update_reset("+income.id+")'/>");
			//让分页为空
			$('.pagination').html('');
		}
	});
}

//更新数据提交事件
function income_update(id){
	var oldId = id;
	var daily = $("#daily").val().trim();
	var deptName = $("#deptName").val();
	var employee = $("#employee").val().trim();
	var E = false;
	var D = false;
	
	if(employee !== "" && employee.match("^[\u4e00-\u9fa5]*$")||employee.match("^[A-Z]{1}[a-z]*$")){
		E = true;
	}
	if(daily !== "" && daily.match("^[1-9][1-9]*[0-9]*$")){
		D = true;
	}
	
	if(E && D){
		//数据传送到后台
		$.ajax({
			type:"POST",
			url:"do.income?method=3",
			data:"id="+oldId+"&daily="+daily+"&deptName="+deptName+"&employee="+employee,
			success:function(){
				income_show();
			}
		});
		alert("账单修改成功！");
	}else{
		alert("填写信息有误，请检查！");
	}
}

//更新数据重置事件
function income_update_reset(id){
	income_update_show(id);
}

//添加账单按钮，显示页面
function income_add_show(){
	$.ajax({
		url: "do.income",
		type: "POST",
		async: false,
		data:"method=2",
		dataType: "json",
		success: function(data){
			var html = "";
			var dept = data.dept_show;
			var IncomeId = data.maxId+1;
			//设置表头
			html = "<tr><th>账单编号</th><th>利润</th><th>部门</th><th>经手人</th><th>交易日期</th><th>最后修改日期</th></tr>"
			
			html += "<tr>";
			html += "<td ><input type='text' id='incomeId' value='"+IncomeId+"' disabled></td>";
			html += "<td><input type='text' id='daily'></td>";
			//td表格单元中，添加一个部门下拉列表
			html += "<td><select id='deptName'>";
			for(var i=0;i<dept.length;i++){
				html += "<option value="+dept[i]+">"+dept[i]+"</option>";
			}
			html += "</select></td>";
			html += "<td><input type='text' id='employee'></td>";
			html += "<td>"+new Date().getFullYear()+"-"+(new Date().getMonth()+1)+"-"+new Date().getDate()+"</td>";
			html += "<td>"+new Date().getFullYear()+"-"+(new Date().getMonth()+1)+"-"+new Date().getDate()+"</td>";
			html += "</tr>";
			
			$('.message_main_head').text('收入管理  > 添加');
			$('.data_table').html(html);
			//显示表格操作按钮，添加和重置
			$('#button').html("<input type='button' value='添加' onclick='income_finish()'/><input type='button' value='重置' onclick='income_add_reset()'/>");
			//让分页为空
			$('.pagination').html('');
		}
	});
};

//提交按钮事件
function income_finish(){
	var id = $("#incomeId").val().trim();
	var daily = $("#daily").val().trim();
	var deptName = $("#deptName").val().trim();
	var employee = $("#employee").val().trim();
	var E = false;
	var D = false;
	
	if(employee !== "" && employee.match("^[\u4e00-\u9fa5]*$")||employee.match("^[A-Z]{1}[a-z]*$")){
		E = true;
	}
	if(daily !== "" && daily.match("^[1-9][1-9]*[0-9]*$")){
		D = true;
	}
	//信息无误，提示
	if(E && D){
		$.ajax({
			type:"POST",
			url:"do.income?method=4",
			data:"id="+id+"&daily="+daily+"&deptName="+deptName+"&employee="+employee,
			success:function(){
				income_show();
			}
		});
		alert("账单添加成功！");
	}else{
		alert("填写信息有误，请检查！");
	}
	
};

//添加重置按钮事件
function income_add_reset(){
	//重新回到添加账单状态
	income_add_show();
};